package util;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;

/**
 *
 * @author Tony Shen
 */
public class StockUtils {

    public static String[] getStockInformation(String stockCode) {
        String url = "http://hq.sinajs.cn/list=" + stockCode;
        try {
            URL u = new URL(url);
            byte[] b = new byte[256];
            InputStream in = null;
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            {
                try {
                    in = u.openStream();
                    int i;
                    while ((i = in.read(b)) != -1) {
                        bo.write(b, 0, i);
                    }
                    String result = bo.toString();
                    String[] stocks = result.split(";");
                    return stocks;
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                } finally {
                    if (in != null) {
                        in.close();
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return null;
    }

    public static ByteArrayOutputStream getDailyK(String stockCode) {
        String url = "http://image.sinajs.cn/newchart/daily/n/" + stockCode + ".gif";
        try {
            URL u = new URL(url);
            byte[] b = new byte[256];
            InputStream in = null;
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            in = u.openStream();
            int i;
            while ((i = in.read(b)) != -1) {
                bo.write(b, 0, i);
            }
            return bo;
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return null;
    }
}
